#include"Queue.h"
void INIQnode(QU* pq)
{
	assert(pq);
	pq->size = 0;
	pq->head = NULL;
	pq->tail = NULL;

}
void destroyQnode(QU* pq)
{
	assert(pq);
	if (pq ->head == NULL)
	{
		return;
	}

    if(pq->head == NULL)
    {
        return ;
    }
	Qnode* next = pq->head->next;
	while (next)
	{
		next = pq->head->next;
		free(pq->head);
		pq->head = next;
	}
	free(pq->head);
	pq->head = pq->tail = NULL;
	pq->size = 0;
}

void Queuepop(QU* pq)
{
	assert(pq);
	assert(pq->head);
	if (pq->head == pq->tail)
	{
		free(pq->tail);
		pq->head = pq->tail = NULL;
	}
	else
	{
		Qnode* next = pq->head->next;
		free(pq->head);
		pq->head = next;
	}
	pq->size--;
}

void Queueprint(QU* pq)
{
	assert(pq);
	assert(pq->head);
	Qnode* cur = pq->head;
	while (cur != pq->tail->next)
	{
		printf("%d->", cur->X);
		cur = cur->next;
	}
	printf("\n");
}



void Queuepush(QU* pq, QUdata x)
{
	Qnode* newnode = (Qnode*)malloc(sizeof(Qnode));
	if (newnode == NULL)
	{
		perror("malloc fail");
		return;
	}
	newnode->next = NULL;
	newnode->X = x;
	if (pq->head == NULL)
	{
		pq->head =pq->tail= newnode;
	}
	else
	{
		pq->tail->next = newnode;
		pq->tail = newnode;
	}
	pq->size++;
}


int	sizeQueue(QU* pq)
{
	assert(pq);
	return pq->size;
}


bool emptyQueue(QU* pq)
{
	assert(pq);
	return pq->size == 0;
}


QUdata Queueback(QU* pq)
{
	assert(pq);
	assert(pq->tail);
	return pq->tail->X;
}
QUdata Queuefront(QU* pq)
{
	assert(pq);
	assert(pq->head);
	return pq->head->X;
}


